class OrganizationsController < ApplicationController
	include AuthenticatedSystem

	layout "main"
	
	before_filter :login_required, :except => [:index, :show, :search]

	def index
		@organizations = Organization.find(:all, :order => "russian_title, foreign_title ASC")
	end
	
	def new
		@countries = Country.find(:all)		
		@organization = Organization.new
		@organization.build_org_logo
		@organization.build_org_photo		
		3.times { @organization.people.build }
		@organization.people.each { |person| person.build_person_photo }
	end
	
	def create
		@organization = Organization.new params[:organization]		
		@organization.user = current_user
		if @organization.save
			redirect_to :action => "index"
		else
			render :action => "new"
		end
	end
	
	def edit
		@countries = Country.find(:all)
		@organization = Organization.find(params[:id])
	end
	
	def update		
		# render :text => params.to_a.to_yaml
		
		@organization = Organization.update(params[:organization_id], params[:organization])
		
		if @organization.save
			redirect_to :action => "index"
		else
			render :action => "edit"
		end
	end
	
	def show
		@organization = Organization.find(params[:id])
	end
	
	def destroy
		Organization.destroy(params[:id])
		redirect_to :action => "index"
	end
	
	def search	  
		@countries = Country.find(:all)
	
		@res = []
		
		if params[:q]
		  ###
		  # Непосредственно поиск
		  ###
		  
		  # Контекстный поиск
		  unless params[:q][:context].nil?
		    @context_organizations = Organization.context_search(params[:q][:context])
		  else
		    @context_organizations = []
		  end
			
			# Поиск по стране юридической регистрации
			unless params[:q][:place].nil? and params[:q][:place].empty?
			  @organizations = Organization.find(:all, :conditions => ["place = ?", params[:q][:place]])
		  else
		    @organizations = []
		  end
		  		
			# Поиск по деятельности
			unless params[:q][:activity_ids].nil?
			  @take_parts = TakePart.find(:all, :conditions => ["activity_id IN (?)", params[:q][:activity_ids]])
			else
			  @take_parts = []
			end
			
		  @take_parts_oragnizations = []
		  @take_parts.each do |x|
		    @take_parts_oragnizations << x.organization
		  end
		  		  
		  # Поиск по георгафии деятельности
		  unless params[:q][:country_ids].nil?
		    @geography = Organization.find_by_sql(["select * from organizations as o, countries_organizations as c where c.country_id in (?) and c.organization_id = o.id", params[:q][:country_ids]])
		  else
		    @geography = []
		  end
		  
		  		  
		  
		  ###
		  # Объединение элементов поиска
		  ###
		  
  		if params[:q][:mode] == "and"
  		  
  		  @res = []
  		  
  		  if @res == []
  		    @res = @context_organizations unless @context_organizations == []
  		  else
  		    @res = @res & @context_organizations unless @context_organizations == []
  		  end
  		  
  		  if @res == []
  		    @res = @organizations unless @organizations == []
  		  else
  		    @res = @res & @organizations unless @organizations == []    
  		  end
  		  
  		  if @res == []
  		    @res = @take_parts_oragnizations unless @take_parts_oragnizations == []
  		  else
  		    @res = @res & @take_parts_oragnizations unless @take_parts_oragnizations == []
  		  end
  		  
  		  if @res == []
  		    @res = @geography unless @geography == []
  		  else
  		    @res = @res & @geography unless @geography == []
  		  end
  		  
  		else
  		  
  		  @res = @organizations | @take_parts_oragnizations | @geography | @context_organizations | []
  		
  		end
		end
	end
	
	def export
		render_text Organization.find(:all).to_xml
	end
	
	protected
	def to_like_conditions( conditions )
     like_conditions = []
      key_count = conditions.size
      k = ""
      conditions.each_key do |key|
        k += "#{key} LIKE ?"
        if key_count > 1 
          k += " and "
        end
        key_count -= 1
      end
      like_conditions << k

      conditions.each_value do |value| 
        like_conditions << "%#{value}%"
      end

      like_conditions
  end
end
